package com.example.dayday.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.dayday.common.R;
import com.example.dayday.pojo.Teacher;
import com.example.dayday.pojo.TeacherCourse;
import com.example.dayday.pojo.User;
import com.example.dayday.service.TeacherCourseService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Helen
 * @since 2022-09-07
 */
@RestController
@RequestMapping("/teacherCourse")
public class TeacherCourseController {

    @Autowired
    private TeacherCourseService teacherCourseService;

    @DeleteMapping("/delete")
    public R deleteByTeacherIdAndCourseId(String teacherId, String courseId, HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user.getRole()!=2){
            return R.error("权限不足");
        }
        Teacher teacher = (Teacher) user;
        if(!"1".equals(teacher.getIsAdmin())){
            return R.error("权限不足");
        }

        if(StringUtils.isEmpty(teacherId)||StringUtils.isEmpty(courseId)){
            return R.error("请同时传入teacherId和courseId");
        }

        LambdaQueryWrapper<TeacherCourse> teacherCourseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        teacherCourseLambdaQueryWrapper.eq(TeacherCourse::getCourseId,courseId);
        teacherCourseLambdaQueryWrapper.eq(TeacherCourse::getTeacherId,teacherId);
        boolean remove = teacherCourseService.remove(teacherCourseLambdaQueryWrapper);
        if (remove){
            return R.ok().message("删除成功");
        }
        return R.error("删除失败");
    }

    @PostMapping("/add")
    public R addByTeacherIdAndCourseId(String teacherId, String courseId, HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user.getRole()!=2){
            return R.error("权限不足");
        }
        Teacher teacher = (Teacher) user;
        if(!"1".equals(teacher.getIsAdmin())){
            return R.error("权限不足");
        }
        if(StringUtils.isEmpty(teacherId)||StringUtils.isEmpty(courseId)){
            return R.error("请同时传入teacherId和courseId");
        }
        TeacherCourse teacherCourse = new TeacherCourse();
        teacherCourse.setCourseId(Long.valueOf(courseId));
        teacherCourse.setTeacherId(Long.valueOf(teacherId));
        LambdaQueryWrapper<TeacherCourse> teacherCourseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        teacherCourseLambdaQueryWrapper.eq(TeacherCourse::getCourseId,courseId);
        teacherCourseLambdaQueryWrapper.eq(TeacherCourse::getTeacherId,teacherId);
        TeacherCourse one = teacherCourseService.getOne(teacherCourseLambdaQueryWrapper);
        if(one!=null){
            return R.error("该老师以任该课程");
        }

        boolean add = teacherCourseService.save(teacherCourse);
        if (add){
            return R.ok().message("添加成功");
        }
        return R.error("添加失败");
    }
}

